/**
 * 费用明细
 * @return
 */

function getMap(mapstr){
	if(mapstr == null || mapstr == '')
		return null;
	var tmp1 = mapstr.split('$');
	var arr = eval(tmp1);
	var map = new HashMap();
	for(var i=0;i<arr.length;i++){
		if(arr[i] == null || arr[i] == '')
			continue;
		var tmp2 = arr[i].split(':');
		if(tmp2[1] == null || tmp2[1] == 'null')
			tmp2[1] = '';
		map.put(tmp2[0],tmp2[1]);
	}
	return map;
}
function adjustSpliter()
{
	var form = getComponent("headForm");
	var headHeight = form.getHeight() + 30;
	var headSpliter = getComponent("spliter1");
	headSpliter.fixProp(headHeight);
}

function afterPageInit() 
{
	var headDs = getDataset('headDataset');
	headDs.setRowSelected(0);

	headDs.onAfterDataChange = function(event, pageIndex)
	{	
		var showTypeIndex = headDs.nameToIndex("showtype");
		var booktypeIndex = headDs.nameToIndex("booktype");
		var grid = getComponent("bodyGrid"), row = event.currentRow;
		// 当前帐页格式值
		var bookTypeValue = row.getCellValue(booktypeIndex);
		if(event.cellColIndex == showTypeIndex)
		{	
			var showTypeValue = row.getCellValue(showTypeIndex);
			setGridVisibleHeaders(window.qryObjs, bookTypeValue, showTypeValue);
		}
		else if(event.cellColIndex == booktypeIndex){
			if(window.isFromQuery){
			    window.isFromQuery = false;
  	  			return;
  	  		}
			if(event.oldValue != null && window.qryConditionObj != null){
				if(event.oldValue != event.currentValue){
	  				var param = "report_query_param=" + window.qryConditionObj;
	  				if(event.currentValue == '1')
	  	  				param += "&fromWhere=jes";
					var pDs = getDataset('bodyDataset');
					var loader = new DatasetLoader(pDs);
					loader.setKeyValue(Dataset.MASTER_KEY);
					loader.setLoadParam(param);
					loader.load();
				}
  			}
			var bookTypeValue = row.getCellValue(booktypeIndex);
			setGridVisibleHeaders(window.qryObjs, bookTypeValue, row.getCellValue(showTypeIndex));
		}
	}
}

function setGridVisibleHeaders(qryObjs, bookType, showType)
{	
 	var visibles = getGridAllVisibleHeaderIds(qryObjs, bookType, showType);
 	
	var showHeaders = [], grid = getComponent("bodyGrid");

 	for(var i = 0, count = grid.basicHeaders.length; i < count; i++)
	{
 		if(visibles.indexOf(grid.basicHeaders[i].keyName) != -1)
			showHeaders.push(grid.basicHeaders[i].keyName);
		}
 	if(showHeaders.length > 0)
		grid.setShowColumns(showHeaders);
}

/**
 * qryObjs:当前的查询对象数组
 * bookType:帐页格式,"0":"本币";"1":"原币","本币"
 * showType:0:名称,"1":编码,"2":名称+编码
 */
function getGridAllVisibleHeaderIds(qryObjs, bookType, showType)
{
 	var headers = [];
 	if(bookType == "0")
 	{
		headers.push("bodydate");
		headers.push("explanation");
		headers.push("billtype");
		headers.push("billno");
		headers.push("currtypename");
 	 	headers.push("debitlocalamount");
 	 	var headDs = getDataset('headDataset');
		var corpindex = headDs.nameToIndex("headcorp");
		var corpvalue = headDs.getSelectedRow().getCellValue(corpindex);
		if(corpvalue != null)  {
			var num = corpvalue.split(",");
			if(num.length > 1) {
				headers.push("corpname");
			}
		}
 	}	
 	else if(bookType == "1")
 	{
 		headers.push("bodydate");
		headers.push("explanation");
		headers.push("billtype");
		headers.push("billno");
 	 	headers.push("currtypename");
 	 	headers.push("debitamount");
 	 	headers.push("debitlocalamount");

 	 	var headDs = getDataset('headDataset');
		var corpindex = headDs.nameToIndex("headcorp");
		var corpvalue = headDs.getSelectedRow().getCellValue(corpindex);
		if(corpvalue != null)  {
			var num = corpvalue.split(",");
			if(num.length > 1) {
				headers.push("corpname");
			}
		}
 	}
 	
 	if(qryObjs != null && qryObjs.length > 0)
 	{
 	 	for(var i = 0; i < qryObjs.length; i++)
 	 	{
 	 		if(qryObjs[i] == 'corpcode'){
		 	 	headers.push("corpname");
		 	 	continue;
		 	 }
			var index = qryObjs[i].substring(qryObjs[0].length - 1);
			if(showType == "1")
				headers.push("qryobjcode" + index);
			else if(showType == "2")
			{
				headers.push("qryobjcode" + index);
				headers.push("qryobjname" + index);
			}
			else 
				headers.push("qryobjname" + index);	
 	 	}
 	}
 	return headers;
}

/**
 * 自定义按钮的执行逻辑
 */
function selfDefBtnFuc(btnNo)
{
	if(btnNo.name == "201")
		doPrint();
	else if(btnNo.name == "202"){
		var url = getCorePath() + "/report/reportquery/reportquery.jsp?pageId=reportquery&pagetype=detail";
		showDialog(url,webbx_trans('yer_query'),'700','500','query_dialog',false,false);
	}else if(btnNo.name == "203"){
		var bodyDs = getDataset('bodyDataset');
		//将明细的查询条件放入session，作为回退到汇总时的条件
		var queryObj =  window.qryConditionObj;
		if(queryObj == null)
			queryObj = getSessionAttribute('total_query_condition');
		var totalIndex = getSessionAttribute('total_query_condition_rowindex');
		if(queryObj == null){
			showMessageDialog(webbx_trans('yer_balanceui002'));
			return;
		}
		var param ="report_query_param=" + encodeURIComponent(queryObj) + "&rowindex=" + totalIndex + "&v_map=" + encodeURIComponent(window.map) + "&other_map=" + encodeURIComponent(window.otherMap);
		var cmd = new CommonCommand('setQryDeteilConToSession');
		cmd.setAsync(false);
		cmd.setParameter(param);
		cmd.exec();
		var url = getCorePath() + '/balancereportui.jsp?model=nc.lfw.finance.model.ExpenseBalanceReportPageModel&ctrl=nc.lfw.finance.report.FyTotalController&pageId=E0100401&triggertype=qrybalance';
		window.location.href = url;	
	}	
}
